package com.coocaa.typhoon.api.v1.userfile.util;

import com.coocaa.cirrus.common.base.exception.BusinessException;
import com.coocaa.cirrus.common.base.util.DateUtil;
import com.coocaa.typhoon.api.third.volcengine.util.VikingDbConvert;
import com.coocaa.typhoon.api.third.volcengine.yml.VolcConfigYml;
import com.coocaa.typhoon.api.v1.userfile.constant.StartTimeSceneConstant;
import com.coocaa.typhoon.api.v1.userfile.model.input.GetSearchDataModel;
import lombok.experimental.UtilityClass;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author bijiahao
 * @date : 2025/9/18 16:22
 * @description
 */
@UtilityClass
public class UserFileParamCheckUtil {

    private static boolean isStartTimeJustNow(String text, List<VolcConfigYml.BackKeywordConfig> backKeywordConfigList) {
//        return text.contains("刚刚");
        return isStartTimeMatch(text, backKeywordConfigList, StartTimeSceneConstant.START_TIME_JUST_NOW);
    }

    private static boolean isStartTimeYesterday0Hour(String text, List<VolcConfigYml.BackKeywordConfig> backKeywordConfigList) {
//        return text.contains("昨天")
//                || text.contains("上次")
//                || text.contains("最近")
//                ;
        return isStartTimeMatch(text, backKeywordConfigList, StartTimeSceneConstant.START_TIME_YESTERDAY_0HOUR);
    }

    private static boolean isStartTimeBeforeYesterday0Hour(String text, List<VolcConfigYml.BackKeywordConfig> backKeywordConfigList) {
//        return text.contains("前天");
        return isStartTimeMatch(text, backKeywordConfigList, StartTimeSceneConstant.START_TIME_BEFORE_YESTERDAY_0HOUR);
    }

    private static boolean isStartTimeLastWeek(String text, List<VolcConfigYml.BackKeywordConfig> backKeywordConfigList) {
//        return text.contains("上周");
        return isStartTimeMatch(text, backKeywordConfigList, StartTimeSceneConstant.START_TIME_LAST_WEEK);
    }

    private static boolean isStartTimeMatch(String text, List<VolcConfigYml.BackKeywordConfig> backKeywordConfigList, String scene) {
        VolcConfigYml.BackKeywordConfig match = backKeywordConfigList.stream().filter(backKeywordConfig -> Objects.equals(backKeywordConfig.getScene(), scene)).findFirst().orElse(null);
        return null != match
                && CollectionUtils.isNotEmpty(match.getWords())
                && match.getWords().stream().anyMatch(text::contains);
    }


    public static boolean isBackKeyword(String text, List<VolcConfigYml.BackKeywordConfig> backKeywordConfigList) {
        if (StringUtils.isBlank(text) || CollectionUtils.isEmpty(backKeywordConfigList)) {
            return false;
        }
        return backKeywordConfigList.stream().anyMatch(backKeywordConfig -> isMatch(backKeywordConfig, text));
    }

    private static boolean isMatch(VolcConfigYml.BackKeywordConfig backKeywordConfig, String text) {
        if (CollectionUtils.isEmpty(backKeywordConfig.getWords())) {
            return false;
        }
        return backKeywordConfig.getWords().stream().anyMatch(text::contains);
    }

    public static void checkSetTime(GetSearchDataModel dataModel, boolean isBackKeyword, List<VolcConfigYml.BackKeywordConfig> backKeywordConfigList) {
        if (!isBackKeyword) {
            return;
        }
        long startTime = DateUtil.toTimestamp(dataModel.getStartDate());
        long endTime = DateUtil.toTimestamp(dataModel.getEndDate());
        if (VikingDbConvert.isTimeCanFilter(startTime) || VikingDbConvert.isTimeCanFilter(endTime)) {
            return;
        }
        String text = dataModel.getText();
        Date currentDate = DateUtil.getCurDate();
        if (isStartTimeJustNow(text, backKeywordConfigList)) {
            //刚刚
            dataModel.setStartDate(DateUtil.toLongString(DateUtil.getPreviouMinute(currentDate, 30)));
            dataModel.setEndDate(DateUtil.toLongString(currentDate));
        } else if (isStartTimeYesterday0Hour(text, backKeywordConfigList)) {
            //昨天
            dataModel.setStartDate(DateUtil.getPreviouDate0Hour(currentDate, 1));
            dataModel.setEndDate(DateUtil.getPreviouDate24Hour(currentDate, 1));
        } else if (isStartTimeBeforeYesterday0Hour(text, backKeywordConfigList)) {
            //前天
            dataModel.setStartDate(DateUtil.getPreviouDate0Hour(currentDate, 2));
            dataModel.setEndDate(DateUtil.getPreviouDate24Hour(currentDate, 2));
        } else if (isStartTimeLastWeek(text, backKeywordConfigList)) {
            //上周
            dataModel.setStartDate(DateUtil.getPreviouDate0Hour(currentDate, 7));
            dataModel.setEndDate(DateUtil.getPreviouDate24Hour(currentDate, 7));
        }
    }


    /**
     * 只保留数字、字母、汉字
     *
     * @param input 输入
     * @return {@link String}
     */
    public static String retainLettersNumbersChinese(String input) {
        if (StringUtils.isEmpty(input)) {
            return input;
        }
        // 正则表达式，匹配非字母、数字和汉字的字符
        String regex = "[^a-zA-Z0-9\u4e00-\u9fa5]";
        // 使用正则表达式替换特殊字符为空字符串
        return input.replaceAll(regex, "");
    }

    public static void check(GetSearchDataModel dataModel) {
        if (StringUtils.isBlank(dataModel.getUserId())) {
            throw new BusinessException("用户ID不能为空");
        }
        if (CollectionUtils.isEmpty(dataModel.getTosLinkList()) && StringUtils.isBlank(dataModel.getText())) {
            throw new BusinessException("文本或者TOS链接不能都为空");
        }
    }
}
